var main = (function () {
    var run = function () {
        // --- Initialize control -------------------------------------------
        {
            $('#tabsLeft').tabs();
            $('#subtab-header').tabs({ active: 1 });
            $('#tabRight').tabs();
            $('#splitter').splitter();
        }

        // --- Initialize header Dynatree -------------------------------------------
        $("#headerTree").dynatree({
            initAjax: {
                url: "api/rules"
            },
            onActivate: function (node) {
                logger.warning("You selected node '" + node.data.title + "'");
            },
            onDeactivate: function (node) {
                //todo: your code here
            },
            dnd: {
                revert: false, // true: slide helper back to source if drop is rejected
                onDragStart: function (node) {
                    /** This function MUST be defined to enable dragging for the tree.
                     *  Return false to cancel dragging of node.
                     */
                    if (node.data.isFolder) {
                        return false;
                    }
                    return true;
                },
                onDragStop: function (node) {
                    //todo: your code here
                }
            },
            classNames: {
                nodeIcon: ''
            }
        });

        // --- Initialize list Dynatree -------------------------------------------
        $("#listTree").dynatree({
            initAjax: {
                url: "api/list/get"
            },
            checkbox: true,
            onActivate: function (node) {
                //todo: your code here
            },
            onDeactivate: function (node) {
                //todo: your code here
            },
            dnd: {
                autoExpandMS: 1000,
                preventVoidMoves: true, // Prevent dropping nodes 'before self', etc.
                onDragEnter: function (node, sourceNode) {
                    /* sourceNode may be null for non-dynatree droppables.
                     * Return false to disallow dropping on node. In this case
                     * onDragOver and onDragLeave are not called.
                     * Return 'over', 'before, or 'after' to force a hitMode.
                     * Any other return value will calc the hitMode from the cursor position.
                     */

                    // For the sake of this example deny dropping over folders
                    if (node.data.isFolder) {
                        return false;
                    }
                    return true;
                    //                return "over";
                },
                onDragOver: function (node, sourceNode, hitMode) {
                    /* Return false to disallow dropping this node.*/
                    //         if(node.data.isFolder){
                    //           var dd = $.ui.ddmanager.current;
                    //           dd.cancel();
                    //           alert("folder");
                    //         }
                    //todo: your code here
                },
                onDrop: function (node, sourceNode, hitMode, ui, draggable) {
                    /* This function MUST be defined to enable dropping of items on the tree.
                     * sourceNode may be null, if it is a non-Dynatree droppable.
                     */
                    var copynode;
                    if (sourceNode) {
                        copynode = sourceNode.toDict(true, function (dict) {
                            dict.title = dict.title;
                            delete dict.key; // Remove key, so a new one will be created
                        });
                    } else {
                        copynode = { title: "This node was dropped here (" + ui.helper + ")." };
                    }
                    if (hitMode == "over") {
                        // Append as child node
                        node.addChild(copynode);
                        // expand the drop target
                        node.expand(true);
                    } else if (hitMode == "before") {
                        // Add before this, i.e. as child of current parent
                        node.parent.addChild(copynode, node);
                    } else if (hitMode == "after") {
                        // Add after this, i.e. as child of current parent
                        node.parent.addChild(copynode, node.getNextSibling());
                    }
                },
                onDragLeave: function (node, sourceNode) {
                    /** Always called if onDragEnter was called.
                     */
                    //todo: your code here
                }
            },
            classNames: {
                nodeIcon: ''
            }
        });


        //jquery event handler
        {
            // --- Refesh tree -------------------------------------------
            $('#btnRefreshHeader').click(function () {
                var children = repository.rule.getAll();
                $("#headerTree").dynatree("getRoot").fromDict({ children: children });
            });

            // --- Save List tree -------------------------------------------
            $('#btnSaveList').click(function () {
                var dataJS = $("#listTree").dynatree("getTree").toDict();
                var dataJSON = ko.toJSON(dataJS);
                repository.list.save(dataJSON);
            });

            // --- Refesh tree -------------------------------------------
            $('#btnResetList').click(function () {
                var children = repository.list.getAll();
                $("#listTree").dynatree("getRoot").fromDict({ children: children });
                logger.warning("Refresh List tree");
            });
        }
    };

    return { run: run };
})();